package com.ruoyi.common.utils;

/*
  @Description 返回包装类
 * @Author GSX
 * @Date Created in 2022/5/5
 */
import com.ruoyi.common.enums.HttpStatusEnum;
import lombok.Data;

/**
 * 返回包装类
 */
@Data
public class Result<T> {

    public Result(boolean success, int code) {
        this.setSuccess(success);
        this.setCode(code);
    }

    public Result(boolean success, int code, T data) {
        this.setSuccess(success);
        this.setCode(code);
        this.setData(data);
    }

    public Result(boolean success, int code, String msg) {
        this.setSuccess(success);
        this.setCode(code);
        this.setMsg(msg);
    }


    /**
     * 请求是否成功
     * true:成功
     * false：失败
     */
    private boolean success;

    /**
     * 状态码
     * 成功：200
     * 失败：其他
     */
    private int code;

    /**
     * 失败状态码描述
     * 如果成功不返回
     * 失败返回状态码对应的msg消息
     */
    private String msg;

    /**
     * 请求数据的结果
     */
    private T data;

    public static <T> Result<T> success() {
        return new Result<>(true, 200);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, 200, data);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<>(true, 200, msg);
    }

    public static <T> Result<T> fail(HttpStatusEnum httpStatusEnum) {
        return new Result<>(false, httpStatusEnum.code(), httpStatusEnum.reasonPhraseCN());
    }

    public static <T> Result<T> fail() {
        return new Result<>(false, HttpStatusEnum.NETWORK_EXCEPTION.code(), HttpStatusEnum.NETWORK_EXCEPTION.reasonPhraseCN());
    }

    public static <T> Result<T> fail(T data) {
        return new Result<>(false, HttpStatusEnum.NETWORK_EXCEPTION.code(), data);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(false, HttpStatusEnum.NETWORK_EXCEPTION.code(), msg);
    }

    public static <T> Result<T> fail(HttpStatusEnum httpStatusEnum, String msg) {
        return new Result<>(false, httpStatusEnum.code(), msg);
    }


}
